//
//  ZCADViewController.m
//  BaiSi
//
//  Created by 张灿 on 16/4/27.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCADViewController.h"
#import <AFNetworking.h>
#import "ZCADItem.h"
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "ZCTBC.h"

#define ZCCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface ZCADViewController ()
//启动图片,让用户以为还是启动界面,其实已经跳到广告界面了
@property (weak, nonatomic) IBOutlet UIImageView *lanuchImageV;

//广告占位图
@property (weak, nonatomic) IBOutlet UIImageView *ADImageV;

//模型
@property (nonatomic, strong) ZCADItem *item;

//跳转按钮
@property (weak, nonatomic) IBOutlet UIButton *jumpButton;

//定时器,用来强引用
@property (weak, nonatomic) NSTimer *timer;

@end

@implementation ZCADViewController

//既然要运行时显示广告,就把窗口的根控制器换成AD的

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置启动图片
    [self setupLaunchScreenImage];
    
    //加载广告数据
    [self loadAdData];
    
    //创建定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

#pragma mark - 跳过广告
- (void)timeChange
{
    static NSInteger i = 3;
    i--;
    //拼接倒计时
    [self.jumpButton setTitle:[NSString stringWithFormat:@"跳过(%ld)", i] forState:UIControlStateNormal];
    if (i < 0) {
        [self jumpClick];
    }
}

- (IBAction)jumpClick {
    
    [UIApplication sharedApplication].keyWindow.rootViewController = [[ZCTBC alloc] init];
    //销毁定时器
    [self.timer invalidate];
}

#pragma mark - 加载网络广告数据
- (void)loadAdData
{
    //创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //设置参数
    NSDictionary *dict = @{
                           @"code2" : ZCCode2
                           };
    
    //发送请求GET
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //提取数据里面的广告信息,转换为字典
        NSDictionary *dict = [responseObject[@"ad"] firstObject];
        
        //字典转模型
        ZCADItem *item = [ZCADItem mj_objectWithKeyValues:dict];
        self.item = item;
        
        //显示数据
        //手动创建一个uiimageview
        UIImageView *adImageV = [[UIImageView alloc] init];
        //设置广告图片可交互
        adImageV.userInteractionEnabled = YES;
        
        //判断图片是否有尺寸,有的话就设置frame
        if (item.w) {
            CGFloat h = ZCScreenW / item.w * item.h;
            adImageV.frame = CGRectMake(0, 0, ZCScreenW, h);
        }
        
        //添加点按手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [adImageV addGestureRecognizer:tap];
        
        //通过url加载广告图片
        [adImageV sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        
        //添加到占位图上去
        [self.ADImageV addSubview:adImageV];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - 点击跳转广告链接
//点击广告跳转链接
- (void)tap
{
    NSURL *url = [NSURL URLWithString:self.item.ori_curl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}

#pragma mark - 设置启动图片(屏幕适配)
- (void)setupLaunchScreenImage
{
    //屏幕适配,根据屏幕大小自动加载对应的图片
    if (iPhone6P) { // iPhone6P
        self.lanuchImageV.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iPhone6) {
        self.lanuchImageV.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iPhone5) {
        self.lanuchImageV.image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    } else if (iPhone4) {
        self.lanuchImageV.image = [UIImage imageNamed:@"LaunchImage"];
    }
}

@end
